const mysql = require('mysql2/promise')

const dbConfig = {
  host: 'localhost',
  user: 'root',
  password: 'GKL9512357',
  database: 'novel_platform',
  charset: 'utf8mb4'
}

async function checkUsers () {
  let connection
  try {
    connection = await mysql.createConnection(dbConfig)

    console.log('📊 检查用户数据...\n')

    // 查询所有用户
    const [users] = await connection.execute(
      'SELECT id, username, email, nickname, password_hash FROM users LIMIT 10'
    )

    console.log(`找到 ${users.length} 个用户:`)
    users.forEach(user => {
      console.log(`  ID: ${user.id}`)
      console.log(`  用户名: ${user.username}`)
      console.log(`  邮箱: ${user.email}`)
      console.log(`  昵称: ${user.nickname}`)
      console.log(`  密码哈希: ${user.password_hash ? '已设置' : '未设置'}`)
      console.log('  ---')
    })

    // 测试查找特定用户
    if (users.length > 0) {
      const testUser = users[0]
      console.log(`\n🔍 测试查找用户 "${testUser.email}":`)

      const [foundUsers] = await connection.execute(
        'SELECT * FROM users WHERE email = ? OR username = ?',
        [testUser.email, testUser.email]
      )

      console.log(`  找到 ${foundUsers.length} 个匹配用户`)
      if (foundUsers.length > 0) {
        console.log(`  用户ID: ${foundUsers[0].id}`)
        console.log(`  密码哈希: ${foundUsers[0].password_hash}`)
      }
    }
  } catch (error) {
    console.error('❌ 错误:', error.message)
  } finally {
    if (connection) await connection.end()
  }
}

checkUsers()
